/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actionforms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author safar
 */
public class AssignmentManagementForm extends org.apache.struts.action.ActionForm {

    /** The UUID of the file. */
    private String id;
    /** The form file. */
    private transient FormFile file;

    /**
     * @return the file
     */
    public FormFile getFile() {
        return file;
    }

    /**
     * @param file the file to set
     */
    public void setFile(FormFile file) {
        this.file = file;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Constructor.
     */
    public AssignmentManagementForm() {
        super();
        id = null;
        file = null;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        //Heavy liftin to transmit the file
        if (getFile() != null && id == null) { //Create check
            //If the file size is zero
            if (getFile().getFileSize() == 0) {
                errors.add("file", new ActionMessage("errors.file.required"));
            } /**
             * If the file size is greater than 5 MB.
             */
            else if (getFile().getFileSize() > 1024 * 1024 * 5) {
                errors.add("fileSize", new ActionMessage("errors.file.size", "5"));
            }
        } else if (getFile() != null && id != null) { //Update check
        } else if (getFile() == null && id != null) { //Delete/Retrieve check
        } else { //Unknown combinatnoi return error.
            errors.add("file", new ActionMessage("error.form.invalid"));
        }

        return errors;
    }
}
